package com.zhengjiang.code.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @ClassName PropertiesUtil
 * @Description
 * @Author zhengjiang(99514925 @ qq.com)
 * @Date 2020-06-24 13:30
 * @Version V1.0
 **/
public class PropertiesUtil {

    private static Properties props;
    private final String resourceFile;

    public PropertiesUtil(String resourceFile) throws IOException {
        this.resourceFile = resourceFile;
        props = new Properties();
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(resourceFile);
        InputStreamReader isr = new InputStreamReader(is,"UTF-8");
        props.load(isr);
    }

    public String getProperty(String key){
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value))
            return null;
        return value.trim();
    }

    public String getProperty(String key,String defaultValue){
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value))
            value = defaultValue;
        return value.trim();
    }

}
